<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Menus;
use Illuminate\Support\Facades\Auth;
use App\Repositories\MenusRepository;
use App\Http\Requests\Admin\Menus\ShowRequest;
use App\Http\Requests\Admin\Menus\QueryResource;
use App\Http\Requests\Admin\Menus\MenusCreate;
use App\Http\Requests\Admin\Menus\MenusUpdate;
use App\Http\Requests\Admin\Menus\MenusDestroy;

class MenusController extends Controller
{
    protected $menusRepository;

    /**
     * MenusesController constructor.
     * @param MenusRepository $menusRepository
     */
    public function __construct(
        MenusRepository $menusRepository
    )
    {
        $this->menusRepository = $menusRepository;
    }

    /**
     * 获取菜单列表资源列表
     *
     * Date: 2018/05/10
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryResource $request)
    {
        $service_id = $request->get('service_id', false);
        $limit = $request->get('area_id', false);
        $menuses = $this->menusRepository->getMenuses($service_id, $limit);
        return success($menuses);
    }

    /**
     * 获取菜单详情
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param ShowRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(ShowRequest $request, Menus $menus)
    {
        return success($menus);
    }


    /**
     * 创建菜单
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param MenusCreate $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(MenusCreate $request)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $menuses = $this->menusRepository->create($attributes);
        return stored($menuses);
    }

    /**
     * 更新菜单资源
     *
     * Date: 2018/05/11
     * @author Tenny
     * @param MenusUpdate $request
     * @param menus $menus
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(MenusUpdate $request, Menus $menus)
    {
        $menusesAttr = $request->only([
            'code',
            'parent_code',
            'name',
            'icon',
            'uri',
            'sort',
            'status',
        ]);
        $menus->update($menusesAttr);
        return updated($menus);
    }

    /**
     * 删除 菜单 资源
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param MenusDestroy $request
     * @param Menus $menus
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(MenusDestroy $request, Menus $menus)
    {
        $force = $request->get('force');
        $force ? $menus->delete() : $menus->forceDelete();
        return deleted();
    }
}
